package com.usian.service;

import com.usian.SSOServiceApp;
import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.redis.RedisClient;
import com.usian.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author shkstart
 * @create 2021-01-24 13:53
 */
@Service
@Transactional
public class SSOServiceImpl implements SSOService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${SESSION_EXPIRE}")
    private Long SESSION_EXPIRE;

    @Value("${USER_INFO}")
    private String USER_INFO;

    @Override
    public boolean checkUserInfo(String checkValue, Integer checkFlag){
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        if (checkFlag == 1){
            criteria.andUsernameEqualTo(checkValue);
        }else if (checkFlag == 2){
            criteria.andPhoneEqualTo(checkValue);
        }
        List<TbUser> list = tbUserMapper.selectByExample(example);
        if (list == null || list.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public Integer userRegister(TbUser user){
        //对密码加密处理
        String digest = MD5Utils.digest(user.getPassword());
        user.setPassword(digest);
        //补齐数据
        user.setCreated(new Date());
        user.setUpdated(new Date());
        return this.tbUserMapper.insert(user);
    }

    @Override
    public Map userLogin(String username, String password){
        //判断用户名和密码是否正确
        String pwd = MD5Utils.digest(password);
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        List<TbUser> userList = tbUserMapper.selectByExample(tbUserExample);
        if (userList == null || userList.size() <= 0){
            return null;
        }
        TbUser tbUser = userList.get(0);
        String token = UUID.randomUUID().toString();
        tbUser.setPassword("");
        redisClient.set(USER_INFO + ":" + token,tbUser);
        redisClient.expire(USER_INFO + ":" + token,SESSION_EXPIRE);

        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);
        map.put("userid",tbUser.getId().toString());
        map.put("username",tbUser.getUsername());
        return map;
    }

    @Override
    public TbUser getUserByToken(String token) {
        TbUser tbUser = (TbUser) redisClient.get(USER_INFO + ":" + token);
        if (tbUser != null){
            redisClient.expire(USER_INFO + ":" + token,SESSION_EXPIRE);
            return tbUser;
        }
        return null;
    }

    @Override
    public Boolean logOut(String token) {
        return redisClient.del(USER_INFO + ":" + token);
    }
}
